<?php
namespace jy_carfee;
defined('BY_JYA') or exit('error');
/*
	分销插件工具类
*/
class model extends \plugin{
	
	public function checklogin(){
		cfc('core')->checklogin();
		$this->model('member')->register();
	}
	public function current_level_up_types(){
		return str_array($this->setting['level_up_types']);
	}
	public function current_daili_file_types(){
		return str_array($this->setting['daili_file']);
	}
	public function available_areas(){
		$areas=file_get_contents(ROOT_D.'/core/resource/area_town.txt');
		$areas=unserialize($areas);
		return $areas;
	}
	public function get_area_code($area){
		$address=$area['province'];
		if($area['city']){
			$address.=' '.$area['city'];
		}
		if($area['district']){
			$address.=' '.$area['district'];
		}
		if($area['street']){
			$address.=' '.$area['street'];
		}
		$area_code=pdo_getcolumn('core_area',array('address'=>$address),'code');
		if(!$area_code){
			$isup=pdo_count('core_area',array(),'code');
			if($isup==0){
				require ROOT_D.'/update/update_area.php';
			}
		}
		return $area_code;
	}
	public function addcheblog($post=array(),$isadd=0,$remark=''){
			$data=array(
					'uniacid'=>$_SESSION['uniacid'],
					'tomid'=>$post['tomid'],
					'kuanorderid'=>$post['kuanorderid'],
					'mid'=>$post['mid']?:$_SESSION['uid'],
					'fee'=>$post['fee'],
					'type'=>$post['type'],
					'isadd'=>$isadd,
					'remark'=>$remark,
					'status'=>$post['status']?:0,
					'createtime'=>TIMESTAMP,
			);
			$this->insert('zhiblog',$data);
			return true;
	}
	public function addgongblog($post=array(),$isadd=0,$remark=''){
			$data=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$post['mid']?:$_SESSION['uid'],
					'fee'=>$post['fee'],
					'isadd'=>$isadd,
					'remark'=>$remark,
					'status'=>$post['status']?:0,
					'createtime'=>TIMESTAMP,
			);
			$this->insert('gongblog',$data);
			return true;
	}
	public function addkuanblog($post=array(),$isadd=0,$remark=''){
			$data=array(
					'uniacid'=>$_SESSION['uniacid'],
					'kuanorderid'=>$post['kuanorderid'],
					'mid'=>$post['mid']?:$_SESSION['uid'],
					'fee'=>$post['fee'],
					'type'=>$post['type'],
					'isadd'=>$isadd,
					'remark'=>$remark,
					'status'=>$post['status']?:0,
					'createtime'=>TIMESTAMP,
			);
			$this->insert('kuanblog',$data);
			$id=$this->insertid();
			return $id;
	}
	public function addkuanorder($post=array(),$isadd=0,$remark=''){
			$gongtimes=$this->setting['gongtimes']?:48;
			$gongfee=$this->setting['gongfee']?:4244;
			$data=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$post['mid']?:$_SESSION['uid'],
					'year'=>4,
					'months'=>$gongtimes,
					'fee'=>$gongfee,
					'allfee'=>$gongtimes*$gongfee,
					'createtime'=>TIMESTAMP,
			);
			$this->insert('kuanorder',$data);
			$id=$this->insertid();
			return $id;
	}
	public function upgonglog($kuanorderid=0){
			$year=date('Y');
			$month=date('m');
			$ismonth=$this->get('gonglog',array('month'=>$month,'year'=>$year,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'kuanorderid'=>$kuanorderid),'id');
			if($ismonth){
				return false;
			}
			$no=$this->count('gonglog',array('uniacid'=>$_SESSION['uniacid'],'kuanorderid'=>$newpost['kuanorderid']),'id');
			$no++;
			if($no<12){
				$status=2;
			}else{
				$status=0;
			}
			$newpost=array(
				'mid'=>$_SESSION['uid'],
				'kuanorderid'=>$this->session('kuanorderid'),
				'fee'=>$this->setting['gongfee']?:4244,
				'year'=>$year,
				'month'=>$month,
				'status'=>$status,
			);
			$remark='个人第'.$no.'期月供';
			$this->addgonglog($newpost,0,$remark);
			return true;
	}
	public function addgonglog($post=array(),$type=0,$remark=''){
			$no=$this->count('gonglog',array('uniacid'=>$_SESSION['uniacid'],'kuanorderid'=>$post['kuanorderid']),'id');
			$data=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$post['mid'],
				'gongorderid'=>$post['gongorderid'],
				'kuanorderid'=>$post['kuanorderid'],
				'no'=>$no+1,
				'fee'=>$post['fee'],
				'year'=>$post['year'],
				'month'=>$post['month'],
				'status'=>$post['status'],
				'type'=>$type,
				'remark'=>$remark,
				'createtime'=>TIMESTAMP,
			);
			$this->insert('gonglog',$data);
			return true;
	}
	public function check_cheblog($logid=0,$status=0,$checkmsg=''){
		$log=$this->get('zhiblog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$logid));
		$update=array('status'=>$status);
		if($status==2){
			$update['checkmsg']=$checkmsg;
			//退回去
			if($log['mid']){
				$this->update('member',array('cheb +='=>$log['fee']),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$log['mid']));
			}
		}
		if($status==1){
			if($log['tomid']){
				$this->update('member',array('cheb +='=>$log['fee']),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$log['tomid']));
				$newpost=array(
					'mid'=>$log['tomid'],
					'fee'=>$log['fee'],
					'status'=>1,
				);
				$this->addcheblog($newpost,1,'别人转让的');
			}elseif($log['type']==1){
				$this->update('member',array('gongb +='=>$log['fee']),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$log['mid']));
				$newpost=array(
					'mid'=>$log['mid'],
					'fee'=>$log['fee'],
					'status'=>1,
				);
				$this->addgongblog($newpost,1,'车指标兑换月供标');
			}elseif($log['type']==2){
				$this->update('member',array('kuanb +='=>$log['fee']),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$log['mid']));
				$newpost=array(
					'mid'=>$log['mid'],
					'fee'=>$log['fee'],
					'status'=>1,
				);
				$this->addkuanblog($newpost,1,'车指标兑换车款标');
			}
		}
		$this->update('zhiblog',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$logid));
		return true;
	}
	public function check_gongblog($logid=0,$status=0,$checkmsg=''){
		$log=$this->get('gongblog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$logid));
		$update=array('status'=>$status);
		if($status==2){
			$update['checkmsg']=$checkmsg;
			//退回去
			if($log['mid']){
				$this->update('member',array('gongb +='=>$log['fee']),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$log['mid']));
			}
		}
		if($status==1){
			if($log['tomid']){
				$this->update('member',array('gongb +='=>$log['fee']),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$log['tomid']));
				$newpost=array(
					'mid'=>$log['tomid'],
					'fee'=>$log['fee'],
					'status'=>1,
				);
				$this->addgongblog($newpost,1,'别人转让的');
			}
		}
		$this->update('gongblog',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$logid));
		return true;
	}
	public function check_kuanblog($logid=0,$status=0,$checkmsg=''){
		$log=$this->get('kuanblog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$logid));
		$update=array('status'=>$status);
		if($status==2){
			$update['checkmsg']=$checkmsg;
			//退回去
			if($log['mid']){
				$this->update('member',array('kuanb +='=>$log['fee']),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$log['mid']));
			}
		}
		if($status==1){
			$post=array(
				'mid'=>$log['mid'],
			);
			$kuanorderid=$this->addkuanorder($post,0,'申请激活年月供计划');
			$update['kuanorderid']=$kuanorderid;
		}
		$this->update('kuanblog',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$logid));
		return true;
	}
	public function check_kuanorder($logid=0,$status=0,$checkmsg=''){
		$order=$this->get('kuanorder',array('uniacid'=>$_SESSION['uniacid'],'id'=>$logid));
		$update=array('status'=>$status);
		if($status==2){
			$update['checkmsg']=$checkmsg;
			//退回去
			if($order['mid']){
				$this->update('member',array('kuanb +='=>1),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$order['mid']));
			}
		}
		if($status==1){
			$newpost=array(
				'mid'=>$order['mid'],
				'kuanorderid'=>$logid,
				'no'=>1,
				'fee'=>$this->setting['gongfee']?:4244,
				'year'=>date('Y'),
				'month'=>date('m'),
				'status'=>0,
			);
			$remark='总部提供第1期月供';
			$this->addgonglog($newpost,0,$remark);
			$update['start_time']=TIMESTAMP;
		}
		$this->update('kuanorder',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$logid));
		return true;
	}
	public function check_gonglog($logid=0,$status=0,$checkmsg=''){
		$log=$this->get('gonglog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$logid));
		$update=array();
		if($status==2){
			$update['checkmsg']=$checkmsg;
			$update['status']=4;
		}
		if($status==0){
			$update['status']=1;
		}
		if($status==1){
			$update['status']=2;
		}
		$this->update('gonglog',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$logid));
		return true;
	}
	public function check_paylog($logid=0,$nopass=0,$checkmsg=''){
		$log=$this->get('gonglog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$logid));
		$update=array();
		if($nopass){
			$update['checkmsg']=$checkmsg;
			$update['status']=4;
		}else{
			$update['status']=3;
		}
		$this->update('gonglog',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$logid));
		return true;
	}
}